package com.suxin.shiro.jwt.controller;

import com.suxin.shiro.jwt.token.JwtToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Tang
 * @classname UserController
 * @description [  ]
 * @date 2021/3/21 11:41
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/login")
    public JsonResult login(String username,String password) {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new JwtToken(username,password));
        } catch (Exception e) {
            return null;
        }
        Map<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("id", 10);
        hashMap.put("username", "tang");
        JsonResult<Object> jsonResult = new JsonResult<>();
        jsonResult.setData(JwtUtils.generateToken(hashMap));
        return jsonResult;
    }

    @GetMapping("/logOut")
    public String logOut() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "logOut success...";
    }

    @RequiresRoles(value = {"admin"})
    @GetMapping("/userInfoAdmin")
    public String userInfoAdmin() {
        return "userInfo...";
    }

    @RequiresRoles(value = {"test"})
    @GetMapping("/userInfoTest")
    public String userInfoTest() {
        return "userInfo...";
    }


    @GetMapping("/error")
    public String loginError() {
        return "loginError...";
    }

}